<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;
// 应用公共文件
/*
 * 判断是否是微信
 * 赵坤
 * 20161120
 * */
function is_weixin(){
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
        return true;
    }
    return false;
}
/*
 * 判断是否是手机
 * 赵坤
 * 20161120
 * */
function is_phone(){
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
        //从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }else{
        return false;
    }
}
/**
 * 验证手机号是否正确
 * @author 赵坤
 * @param INT $mobile
 */
function checkphone($mobile){
    if (!is_numeric($mobile)) {
        return false;
    }
    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
}
/*
 * 获取上传图片模版(多图)
 * 赵坤
 * 20161214
 * */
function tplImage($upname){
    $seller_data = Db::name('shop_seller')->where(array('seller_id'=>session('shop')))->find();
    $seller_set = unserialize($seller_data['seller_set']);
    $water_js = 'var imgLink="";';
    if(isset($seller_set['is_water'])&&$seller_set['is_water']==1){
        if($seller_set['water_type']==1&&!empty($seller_set['water_data'])){
            //图片
            $water_js='
            var imgLink = Qiniu.watermark({
             mode: 1,  
             image: "'.$seller_set['water_data']['content'].'", 
             dissolve: 50,          
             gravity: "'.$seller_set['water_data']['direction'].'", 
             dx: 100, 
             dy: 100   
         });';
        }elseif($seller_set['water_type']==2&&!empty($seller_set['water_data'])){
            //文字
            $water_js = '
            var imgLink = Qiniu.watermark({
             mode: 2,  // 文字水印
             text: "'.$seller_set['water_data']['content'].'", // 水印文字，mode = 2时，必需
             dissolve: 50,          // 透明度，取值范围1-100，非必需，下同
             gravity: "'.$seller_set['water_data']['direction'].'",
             fontsize: 500,        
             font : "黑体",         
             dx: 100,  
             dy: 100,  
             fill: "#B9B6B6"
            });';
        }
    }

    $tem_img = '
    <div class="col-lg-1 col-sm-1 col-xs-1" ><a id="water_show_model_button_div'.$upname.'" href="javascript:void(0);" onclick="showmodel_'.$upname.'(this);"><img  src="__PUBLIC__/admin/images/image.png" alt="点击选择上传"/></a></div>
    <div class="modal fade" id="water_upload_img_'.$upname.'_modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">x</span></button>
                    <h4 class="modal-title text-center"><span class="fa fa-picture-o"></span> 选择图片</h4>
                </div>
                <div class="modal-body">
                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#water_modal_home_img_'.$upname.'" role="tab" data-toggle="tab">上传图片</a></li>
                        <li role="presentation"><a href="#water_modal_head_img_'.$upname.'" role="tab" data-toggle="tab">头像</a></li>
                        <li role="presentation"><a href="#water_modal_goods_img_'.$upname.'" role="tab" data-toggle="tab">商品</a></li>
                        <li role="presentation"><a href="#water_modal_other_img_'.$upname.'" role="tab" data-toggle="tab">其他</a></li>
                    </ul>

                    <!-- Tab panes -->
                    <div class="tab-content" style="min-height: 256px;">
                        <div role="tabpanel" class="tab-pane active" id="water_modal_home_img_'.$upname.'">
                            <div class="row " style="min-height: 250px;padding-top:15px ;border: 3px dashed #e6e6e6;" id="water_upload_img_'.$upname.'_container">
                                <div id= "water_up_div_'.$upname.'">
                                    <div class="col-lg-4 col-sm-4 col-xs-4 col-lg-offset-4">
                                        <br/>
                                        <br/>
                                        <img src="__PUBLIC__/admin/images/image.png" class=" img-responsive center-block"/>
                                    </div>
                                    <div class="col-lg-4 col-sm-4 col-xs-4 col-lg-offset-4 ">
                                        <br/>
                                        <a class="btn btn-info  center-block" id="upload_btn_'.$upname.'">
                                            <span class="fa fa-plus-square"></span> 点击上传
                                        </a>
                                    </div>
                                    <div class="col-lg-12 col-sm-12 col-xs-12 text-center"><h4>或将图片拖拽到此</h4></div>
                                </div>
                                <div id= "water_uplist_div_'.$upname.'" >
                                   <div class="col-lg-2 col-sm-2 water-up-2">
                                       <a href="javascript:void(0);" id="upload_btn_'.$upname.'_list">
                                            <img src="__PUBLIC__/admin/images/image.png" class="img-responsive">
                                       </a>
                                    </div>
                                </div>
                                
                            </div>
                            
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_head_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>1),array('ajaxfunc'=>'water_select_head_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_goods_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>2),array('ajaxfunc'=>'water_select_goods_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_other_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>3),array('ajaxfunc'=>'water_select_other_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="'.$upname.'_sure_btn">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <script type="text/javascript">
    function water_select_other_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>3,'ajax_function'=>'water_select_other_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_other_img_'.$upname.'").empty();
                $("#water_modal_other_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
    
    function water_select_goods_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>2,'ajaxfunc'=>'water_select_goods_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_goods_img_'.$upname.'").empty();
                $("#water_modal_goods_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
    
    function water_select_head_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>1,'ajaxfunc'=>'water_select_head_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_head_img_'.$upname.'").empty();
                $("#water_modal_head_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
     
     
    var '.$upname.'="";
    var '.$upname.'_list ="";
    
    function water_delete_up_img(obj){
        $(obj).parent().remove();
    }
    
    function water_model_img_select_'.$upname.'(obj){
        '.$water_js.'
        var img = $(obj).data(\'value\');
               //单机选择 选择关闭模态框
        var img_div = \'<div class="col-lg-1 col-sm-1 water-up-2"><input type="hidden" name="'.$upname.'" value="\'+img+\'"><img src="\'+img+\'" width="70px" height="70px">\'+
        \'<a class="badge img-badge" onclick="water_delete_up_img(this);"><span class="fa fa-times" ></span></a></div>\';
        $("#water_show_model_button_div'.$upname.'").parent().parent().find("div[class=\'row\']:first").append(img_div);
        toastTop("添加成功,请关闭弹出框查看或继续添加");       
                   
    }
    
    function showmodel_'.$upname.'(obj) {
        if('.$upname.'){
            '.$upname.'.destroy();
            '.$upname.'_list.destroy();
        }
        '.$upname.' =imageUpload("upload_btn_'.$upname.'","'.WebUrl('admin/getQiniuToken',array('img_type'=>$upname)).'","'.config('qiniu_bucket_url').'","water_upload_img_'.$upname.'",{"multi_selection":true,"container":"water_upload_img_'.$upname.'_container","up":$("#water_up_div_'.$upname.'"),"uplist":$("#water_uplist_div_'.$upname.'")});
        '.$upname.'_list =imageUpload("upload_btn_'.$upname.'_list","'.WebUrl('admin/getQiniuToken',array('img_type'=>$upname)).'","'.config('qiniu_bucket_url').'","water_upload_img_'.$upname.'",{"multi_selection":true,"container":"water_upload_img_'.$upname.'_container","up":$("#water_up_div_'.$upname.'"),"uplist":$("#water_uplist_div_'.$upname.'")});
        
        $("#water_uplist_div_'.$upname.'").hide();
        $("#water_up_div_'.$upname.'").show();

        $("#water_upload_img_'.$upname.'_modal").modal("show");
        //点击确定
        $("#'.$upname.'_sure_btn").unbind("click").click(function(){
            '.$water_js.'
            var img_obj = $("#water_uplist_div_'.$upname.'").find("img[data-value]");
            if(img_obj.length>0){
                img_obj.each(function(key,value){
                        var img = $(value).data("value");
                        var img_div = \'<div class="col-lg-1 col-sm-1 water-up-2"><input type="hidden" name="'.$upname.'" value="\'+img+\'"><img src="http://'.config('qiniu_bucket_url').'/\'+img+\'?\'+imgLink+\'" width="70px" height="70px">\'+
                                \'<a class="badge img-badge" onclick="water_delete_up_img(this);"><span class="fa fa-times"></span></a></div>\';
                        $(obj).parent().parent().find("div[class=\'row\']:first").append(img_div);
                        $(value).parent().remove();
                });
            }
             $("#water_upload_img_'.$upname.'_modal").modal(\'hide\');
        });
    }</script>';


    echo $tem_img;
    return;
}
/*
 * 获取单图上传图片模版
 * 赵坤
 * 20161214
 * */
function tplSingleImage($upname){

    $seller_data = Db::name('shop_seller')->where(array('seller_id'=>session('shop')))->find();
    $seller_set = unserialize($seller_data['seller_set']);
    $water_js = 'var imgLink="";';
    if(isset($seller_set['is_water'])&&$seller_set['is_water']==1){
        if($seller_set['water_type']==1&&!empty($seller_set['water_data'])){
            //图片
            $water_js='
            var imgLink = Qiniu.watermark({
             mode: 1,  
             image: "'.$seller_set['water_data']['content'].'", 
             dissolve: 50,          
             gravity: "'.$seller_set['water_data']['direction'].'", 
             dx: 100, 
             dy: 100   
         });';
        }elseif($seller_set['water_type']==2&&!empty($seller_set['water_data'])){
            //文字
            $water_js = '
            var imgLink = Qiniu.watermark({
             mode: 2,  // 文字水印
             text: "'.$seller_set['water_data']['content'].'", // 水印文字，mode = 2时，必需
             dissolve: 50,          // 透明度，取值范围1-100，非必需，下同
             gravity: "'.$seller_set['water_data']['direction'].'",
             fontsize: 500,        
             font : "黑体",         
             dx: 100,  
             dy: 100,  
             fill: "#B9B6B6"
            });';
        }
    }

    $tem_img = '
    <div class="col-lg-1 col-sm-1 col-xs-1" ><a id="water_show_model_button_div'.$upname.'" href="javascript:void(0);" onclick="showmodel_'.$upname.'(this);"><img  src="__PUBLIC__/admin/images/image.png" alt="点击选择上传"/></a></div>
    <div class="modal fade" id="water_upload_img_'.$upname.'_modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">x</span></button>
                    <h4 class="modal-title text-center"><span class="fa fa-picture-o"></span> 选择图片</h4>
                </div>
                <div class="modal-body">
                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#water_modal_home_img_'.$upname.'" role="tab" data-toggle="tab">上传图片</a></li>
                        <li role="presentation"><a href="#water_modal_head_img_'.$upname.'" role="tab" data-toggle="tab">头像</a></li>
                        <li role="presentation"><a href="#water_modal_goods_img_'.$upname.'" role="tab" data-toggle="tab">商品</a></li>
                        <li role="presentation"><a href="#water_modal_other_img_'.$upname.'" role="tab" data-toggle="tab">其他</a></li>
                    </ul>

                    <!-- Tab panes -->
                    <div class="tab-content" style="min-height: 256px;">
                        <div role="tabpanel" class="tab-pane active" id="water_modal_home_img_'.$upname.'">
                            <div class="row " style="min-height: 250px;padding-top:15px ;border: 3px dashed #e6e6e6;" id="water_upload_img_'.$upname.'_container">
                                <div id= "water_up_div_'.$upname.'">
                                    <div class="col-lg-4 col-sm-4 col-xs-4 col-lg-offset-4">
                                        <br/>
                                        <br/>
                                        <img src="__PUBLIC__/admin/images/image.png" class=" img-responsive center-block"/>
                                    </div>
                                    <div class="col-lg-4 col-sm-4 col-xs-4 col-lg-offset-4 ">
                                        <br/>
                                        <a class="btn btn-info  center-block" id="upload_btn_'.$upname.'">
                                            <span class="fa fa-plus-square"></span> 点击上传
                                        </a>
                                    </div>
                                    <div class="col-lg-12 col-sm-12 col-xs-12 text-center"><h4>或将图片拖拽到此</h4></div>
                                </div>
                                <div id= "water_uplist_div_'.$upname.'" >
                                   <div class="col-lg-2 col-sm-2 water-up-2">
                                       <a href="javascript:void(0);" id="upload_btn_'.$upname.'_list">
                                            <img src="__PUBLIC__/admin/images/image.png" class="img-responsive">
                                       </a>
                                    </div>
                                </div>
                                
                            </div>
                            
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_head_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>1),array('ajaxfunc'=>'water_select_head_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_goods_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>2),array('ajaxfunc'=>'water_select_goods_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                        <div role="tabpanel" class="tab-pane" id="water_modal_other_img_'.$upname.'">
                            '.getPic(array('aclass_id'=>3),array('ajaxfunc'=>'water_select_other_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="'.$upname.'_sure_btn">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <script type="text/javascript">
    function water_select_other_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>3,'ajax_function'=>'water_select_other_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_other_img_'.$upname.'").empty();
                $("#water_modal_other_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
    
    function water_select_goods_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>2,'ajaxfunc'=>'water_select_goods_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_goods_img_'.$upname.'").empty();
                $("#water_modal_goods_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
    
    function water_select_head_'.$upname.'(obj){
       var page = $(obj).data(\'page\');
       $.post("'.WebUrl('wateradmin/admin/getPic',array('type'=>1,'ajaxfunc'=>'water_select_head_'.$upname,'select_btn_func'=>'water_model_img_select_'.$upname)).'",{"page":page},function(data){
             if(data.status==1){
                $("#water_modal_head_img_'.$upname.'").empty();
                $("#water_modal_head_img_'.$upname.'").append(data.info);
             }
       },"json");
    }
     
     
    var '.$upname.'="";
    var '.$upname.'_list ="";
    
    function water_delete_up_img(obj){
        $(obj).parent().remove();
    }
    
    function water_model_img_select_'.$upname.'(obj){
        '.$water_js.'
        var img = $(obj).data(\'value\');
               //单机选择 选择关闭模态框
        var img_div = \'<div class="col-lg-1 col-sm-1 water-up-2"><input type="hidden" name="'.$upname.'" value="\'+img+\'"><img src="\'+img+\'" width="70px" height="70px">\'+
        \'<a class="badge img-badge" onclick="water_delete_up_img(this);"><span class="fa fa-times" ></span></a></div>\';
        $("#water_show_model_button_div'.$upname.'").parent().parent().find("div[class=\'row\']:first").empty();        
        $("#water_show_model_button_div'.$upname.'").parent().parent().find("div[class=\'row\']:first").append(img_div);
        $("#water_upload_img_'.$upname.'_modal").modal(\'hide\');             
                   
    }
    
    function showmodel_'.$upname.'(obj) {
        if('.$upname.'){
            '.$upname.'.destroy();
            '.$upname.'_list.destroy();
        }
        '.$upname.' =imageUpload("upload_btn_'.$upname.'","'.WebUrl('admin/getQiniuToken',array('img_type'=>$upname)).'","'.config('qiniu_bucket_url').'","water_upload_img_'.$upname.'",{"multi_selection":true,"container":"water_upload_img_'.$upname.'_container","up":$("#water_up_div_'.$upname.'"),"uplist":$("#water_uplist_div_'.$upname.'")});
        '.$upname.'_list =imageUpload("upload_btn_'.$upname.'_list","'.WebUrl('admin/getQiniuToken',array('img_type'=>$upname)).'","'.config('qiniu_bucket_url').'","water_upload_img_'.$upname.'",{"multi_selection":true,"container":"water_upload_img_'.$upname.'_container","up":$("#water_up_div_'.$upname.'"),"uplist":$("#water_uplist_div_'.$upname.'")});
        
        $("#water_uplist_div_'.$upname.'").hide();
        $("#water_up_div_'.$upname.'").show();

        $("#water_upload_img_'.$upname.'_modal").modal("show");
        //点击确定
        $("#'.$upname.'_sure_btn").unbind("click").click(function(){
            '.$water_js.'
            var img_obj = $("#water_uplist_div_'.$upname.'").find("img[data-value]");
            if(img_obj.length>0){
                img_obj.each(function(key,value){
                    if(key==0){
                        var img = $(value).data("value");
                        var img_div = \'<div class="col-lg-1 col-sm-1 water-up-2"><input type="hidden" name="'.$upname.'" value="\'+img+\'"><img src="http://'.config('qiniu_bucket_url').'/\'+img+\'?\'+imgLink+\'" width="70px" height="70px">\'+
                                \'<a class="badge img-badge" onclick="water_delete_up_img(this);"><span class="fa fa-times"></span></a></div>\';
                        $(obj).parent().parent().find("div[class=\'row\']:first").empty();        
                        $(obj).parent().parent().find("div[class=\'row\']:first").append(img_div);
                        $(value).parent().remove();
                    }else{
                        var img = $(value).data("value");
                        if(img){
                            $(value).parent().remove();
                        }           
                    }
                });
            }
           
            $("#water_upload_img_'.$upname.'_modal").modal(\'hide\');   
        });
    }</script>';
    

    echo $tem_img;
    return;
}
function getPic($where,$func=array('ajaxfunc'=>'','select_btn_func'=>'water_model_img_select'),$page=1,$pagesize=18){

    if(!is_array($where)){
        return false;
    }
    $where['seller_id']=intval(session('shop'));
    $pic_data = Db::name('shop_album_pic')->where($where)->page($page,$pagesize)->select();
    $img_list = '';
    if(!empty($pic_data)){
        $img_list = '<div class="row " id="gallery">';
        foreach ($pic_data as $key=>$value){
            $img_list .= '<div class="col-lg-2 col-sm-2 col-xs-2 water-up-2" >
                              <img class="center-block" src="http://'.$value['apic_cover'].'/'.$value['apic_name'].'" width="70px" height="70px"/>
                               <p class="text-center">'.$value['apic_tag'].'</p>
                               <span class="modal-img-select"></span>     
                               <a class="btn btn-primary  modal-img-select-btn" onclick="'.$func['select_btn_func'].'(this)" data-value="http://'.$value['apic_cover'].'/'.$value['apic_name'].'">选择</a>          
                          </div>';
        }
        $img_list .= '</div>';

        $count = $pic_data = Db::name('shop_album_pic')->where($where)->count();
        if($count%$pagesize!=0){
            $pagecount = intval($count/$pagesize)+1;
        }else{
            $pagecount = $count/$pagesize;
        }

        $first = $page-1;
        if($page==1){
            $li = '<li class="disabled"><span>«</span></li>';
        }else{
            $li = '<li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="1">首页</a></li><li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="'.$first.'">«</a></li>';
        }
        $size = 5;
        if($pagecount>$size&&$page>=$size){
            if($pagecount<$page+$size){
                $size = $pagecount;
            }else{
                $size = $page+$size;
            }
            for($i=$page;$i<=$size;$i++){
                if($page==$i){
                    $li .= '<li class="active"><span>'.$i.'</span></li>';
                }else{
                    $li .= '<li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="'.$i.'">'.$i.'</a></li>';
                }
            }
        }else{
            if($pagecount<$size){
                $size=$pagecount;
            }
            for($i=1;$i<=$size;$i++){
                if($page==$i){
                    $li .= '<li class="active"><span>'.$i.'</span></li>';
                }else{
                    $li .= '<li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="'.$i.'">'.$i.'</a></li>';
                }
            }
        }

        if($page==$pagecount){
            $li .= '<li class="disabled"><span>»</span></li>';
        }else{
            $li .= '<li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="'.++$page.'" >»</a></li><li><a onclick="'.$func['ajaxfunc'].'(this);" data-page="'.$pagecount.'">尾页</a></li>';
        }

        $img_list .='
                <div class="col-md-12 text-center clearfix">
                  <ul class="pagination">
                       '.$li.'
                  </ul>
                </div>
                ';

    }
    return $img_list;
}
/*
 * 获取表名
 * 赵坤
 * 20161211
 * */
function tablename($tablename){
    if(empty($tablename)){
        return false;
    }
    return config('database')['prefix'].$tablename;
}

/*
 * 获取分页
 * 赵坤
 * 20161211  $pagesion = getPage(303,3,10,5);
 * */
function getPage($count,$page=1,$pagesize=15,$size=10){
    if(empty($count)){
        return '';
    }
    if($count%$pagesize!=0){
        $pagecount = intval($count/$pagesize)+1;
    }else{
        $pagecount = $count/$pagesize;
    }
    $param='';
    if(!empty($_GET)){
        $param = '&';
        unset($_GET['page']);
        unset($_GET['pagesize']);
        foreach ($_GET as $key=>$value){
            $param .=$key.'='.$value.'&';
        }
        $param = rtrim($param,'&');
    }

    $first = $page-1;
    if($page==1){
        $li = '<li class="disabled"><span>«</span></li>';
    }else{
        $li = '<li><a href="?page=1'.$param.'">首页</a></li><li><a href="?page='.$first.$param.'">«</a></li>';
    }
    if($pagecount>$size&&$page>=$size){
        if($pagecount<$page+$size){
            $size = $pagecount;
        }else{
            $size = $page+$size;
        }
        for($i=$page;$i<=$size;$i++){
            if($page==$i){
                $li .= '<li class="active"><span>'.$i.'</span></li>';
            }else{
                $li .= '<li><a href="?page='.$i.$param.'">'.$i.'</a></li>';
            }
        }
    }else{
        if($pagecount<$size){
            $size=$pagecount;
        }
        for($i=1;$i<=$size;$i++){
            if($page==$i){
                $li .= '<li class="active"><span>'.$i.'</span></li>';
            }else{
                $li .= '<li><a href="?page='.$i.$param.'">'.$i.'</a></li>';
            }
        }
    }

    if($page==$pagecount){
        $li .= '<li class="disabled"><span>»</span></li>';
    }else{
        $li .= '<li><a href="?page='.++$page.$param.'">»</a></li><li><a href="?page='.$pagecount.$param.'">尾页</a></li>';
    }

    return '<ul class="pagination"> '.$li.'</ul>';
}
/*
 * 获取用户头像
 * 赵坤
 * 20161206
 * */
function toHeadurl($head){
    if(empty($head)){
        $head = config('default_head');
    }else{
        if(stripos($head,'http')!==false){
            return $head;
        }
    }
    return '/../../../upload'.$head;
}
/**
 * 取得IP
 *
 * @return string 字符串类型的返回结果
 */
function getIp(){
    $request = think\Request::instance();
    return $request->ip();
}
/*
 * php获取当前访问的完整url地址
 *
 * 赵坤
 * */
function GetCurUrl() {
    $url = '';
    if ($_SERVER ['SERVER_PORT'] != '80' && $_SERVER ['SERVER_PORT'] != '443') {
        $url .= $_SERVER ['HTTP_HOST'] . ':' . $_SERVER ['SERVER_PORT'] . $_SERVER ['REQUEST_URI'];
    } else {
        $url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];
    }
    // 兼容后面的参数组装
    if (stripos ( $url, '?' ) === false) {
        $url .= '?t=' . time ();
    }
    return $url;
}
/*
 * 获取当前的域名
 *
 * 赵坤
 * */
function GetUrl(){
    if(isset($_SERVER ['HTTP_HOST'])){
        return $_SERVER ['HTTP_HOST'];
    }else{
        return false;
    }
}
/*
 * 获取生成web连接
 *
 * 赵坤
 * */
function WebUrl($url,$param=array(),$domain=false){
    return url($url,$param,'html',$domain);
}
/*
 * 获取生成mobile连接
 *
 * 赵坤
 * */
function MobileUrl($url,$param=array(),$domain=false){
    if(!is_array($param)){
        return false;
    }
    $param['shop'] = session('shop');
    $member_id = intval(session('member_id'));
    if(empty($member_id)){
        $param['parent'] = 0;
    }else{
        $param['parent'] = session('member_id');
    }
    return url('watershop/'.$url,$param,'html',$domain);
}
/*
 * 获取access_token
 *
 * 赵坤
 * */
function getToken($shopid){
    if($shopid<=0){
        return false;
    }
    $access_token = cache('access_token'.$shopid);
    if(empty($access_token)){
        $setInfo = Db::name('shop_setting')->where('seller_id',$shopid)->find();
        $setInfo = unserialize($setInfo['set_value']);
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$setInfo['appid'].'&secret='.$setInfo['appsecret'];
        $content = http_get($url);
        $content = json_decode($content,1);
        if(isset($content['errcode'])){
            return false;
        }else{
            cache('access_token',$content['access_token'],intval($content['expires_in'])-100);
            return $content['access_token'];
        }
    }else{
        return $access_token;
    }
}
/*
 * 获取jsticket
 *
 * 赵坤
 * */
function getJsticket($shopid){
    if($shopid<=0){
        return false;
    }
    $access_token = getToken($shopid);
    $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.$access_token;
    $content = http_get($url);
    $content = json_decode($content,1);
    if($content['ticket']){
        cache('ticket'.$shopid,intval($content['expires_in'])-100);
        return $content['ticket'];
    }else{
       return false;
    }
}
/*
 * 获取code码登录
 *
 * 赵坤
 * */
function Authlogin($userInfo){
    $code = trim($_GET['code']);
    if(empty($code)){
        throw new ErrorException('code获取失败');
    }
    //appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    $param = array(
        'appid'=>$userInfo['appid'],
        'secret'=>$userInfo['secret'],
        'code'=>$code,
        'grant_type'=>'authorization_code'
    );
    $auth_url = ' https://api.weixin.qq.com/sns/oauth2/access_token?'.http_build_query($param);
    $content = file_get_contents ( $auth_url );
    $content = json_decode ( $content, true );
    if(isset($content['errcode'])){
        throw new ErrorException($content['errmsg']);
    }else{
        return $content;
    }
}
/*
 * 获取用户信息
 * 赵坤
 * */
function AuthUser($access_token,$openid){
    $param = array(
        'access_token'=>$access_token,
        'openid'=>$openid,
        'lang'=>'zh_CN'
    );
    $user_url = 'https://api.weixin.qq.com/sns/userinfo?'.http_build_query($param);
    $user_info = http_get($user_url);
    $user_info = json_decode($user_info,true);
    if(!empty($user_info)){
        return $user_info;
    }else{
        throw new ErrorException('获取帐号信息失败');
    }
}
/*
 * 发送客服/模版消息
 * 赵坤
 * 20161123
 * */
function message($content,$type,$templateid = ''){
    $type = intval($type) > 0?intval($type):1;
    $res = 'success';
    if($type==1){
        //客服
        $res = model('common/Weixin')->sendMessage($content, $type);
    }elseif($type==2){
        //模版
        $res = model('common/Weixin')->sendMessage($content, $type,$templateid);
    }
    if('success'===$res){
        return true;
    }else{
        //按商城设置  发送短信或其他消息提醒

    }
}
/*
 * 数组转xml
 * 赵坤
 * 20161122
 * */
function array2xml($arr, $level = 1) {
    $s = $level == 1 ? "<xml>" : '';
    foreach ($arr as $tagname => $value) {
        if (is_numeric($tagname)) {
            $tagname = $value['TagName'];
            unset($value['TagName']);
        }
        if (!is_array($value)) {
            $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
        } else {
            $s .= "<{$tagname}>" . array2xml($value, $level + 1) . "</{$tagname}>";
        }
    }
    $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
    return $level == 1 ? $s . "</xml>" : $s;
}
/*
 * xml转数组
 * 赵坤
 * 20161122
 * */
function xml2array($xml) {
    if (empty($xml)) {
        return array();
    }
    $result = array();
    $xmlobj = isimplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    if($xmlobj instanceof SimpleXMLElement) {
        $result = json_decode(json_encode($xmlobj), true);
        if (is_array($result)) {
            return $result;
        } else {
            return '';
        }
    } else {
        return $result;
    }
}
/**
 * GET 请求
 *
 * @param string $url
 */
function http_get($url) {
    $oCurl = curl_init ();
    if (stripos ( $url, "https://" ) !== FALSE) {
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, FALSE );
    }
    curl_setopt ( $oCurl, CURLOPT_URL, $url );
    curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
    $sContent = curl_exec ( $oCurl );
    $aStatus = curl_getinfo ( $oCurl );
    curl_close ( $oCurl );
    if (intval ( $aStatus ["http_code"] ) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * POST 请求
 *
 * @param string $url
 * @param array $param
 * @return string content
 */
function http_post($url, $param) {
    $oCurl = curl_init ();
    if (stripos ( $url, "https://" ) !== FALSE) {
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
    }
    if (is_string ( $param )) {
        $strPOST = $param;
    } else {
        $aPOST = array ();
        foreach ( $param as $key => $val ) {
            $aPOST [] = $key . "=" . urlencode ( $val );
        }
        $strPOST = join ( "&", $aPOST );
    }
    curl_setopt ( $oCurl, CURLOPT_URL, $url );
    curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $oCurl, CURLOPT_POST, true );
    curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
    $sContent = curl_exec ( $oCurl );
    $aStatus = curl_getinfo ( $oCurl );
    curl_close ( $oCurl );
    if (intval ( $aStatus ["http_code"] ) == 200) {
        return $sContent;
    } else {
        return false;
    }
}